#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;
//refer to http://wansishuang.appspot.com/?p=3002


int getLargestHisto(vector<int> &A)
{
    int maxArea = 0;
    stack<int> myStack;
    int start = -1;

    for (int i = 0; i <= A.size(); i++) {
        if (myStack.empty() || (i != A.size() && A[myStack.top()] <= A[i])) 
            myStack.push(i);
        else if (i == A.size() || A[myStack.top()] > A[i]) {
            while (!myStack.empty() && (i == A.size() -1 || A[myStack.top()] > A[i])) {
                int top = myStack.top();
                myStack.pop();
                int left = myStack.empty() ? -1 : myStack.top();
                int area = (i - left -1) * A[top];
                maxArea = max(area, maxArea);
            }
            myStack.push(i);
        } 
    } 


    return maxArea;
}

int main(int argc, char **argv)
{
    int num;

    while(true) {
        cin >> num;
        if (num == 0) break;
        vector<int> A(num, 0);
        for (int i = 0; i < num; i++) {
            cin >> A[i];
        }
        int area = getLargestHisto(A);
        cout << area << endl;
    }
}
